/**
 * Created by Administrator on 2016/5/17 0017.
 */
$(function(){

    /**
     * 我的信息点击保存事件
     * @author xiaojing
     */
    $('.savaBtn').on('click',function(){
        var self  = $(this).parents('form');
        $.post(self.attr("action"), self.serialize(), function(res){
            //类型分别有 success=成功，error=错误，warning警告
            swal({title:res.info, text: '3秒钟后自动关闭此窗口，如果您不想等待，直接点击确定关闭', type: (res.status) ? 'success' : 'error', timer: '3000'});
        }, "json");
    });

    /**
     * 新增店铺 操作
     * @author xiaojing
     */
    $('.authorizeBtn').on('click',function(){
        var data  = new Array();
        var UserSellerID_value = $("input[name='SellerID']").val();
        var UserAuthToken_value = $("input[name='AuthToken']").val();


        // 获取勾 选 国家 和店铺名称
        $.each($('.removeInputDiv'),function(i,oj){
            var UserMarketCNName_value =  $(oj).find('.UserMarketCNName').val();
            var MarketID_value = $(oj).find('.MarketID').val();
            data[i] =  {UserMarketCNName_value:UserMarketCNName_value,MarketID_value:MarketID_value};
        });

        data =  JSON.stringify(data);

        $.post('index.php?s=/home/index/saveamazonauth',
            {
                data:data,
                UserSellerID_value:UserSellerID_value,
                UserAuthToken_value:UserAuthToken_value
            },function(res){
            console.log(res);
        });
    });

    /**
     * 隐藏显示 店铺 数据
     *  @author xiaojing
     */
    $('.updateAmazonauthShow').on('click',function(){
        var data  = {};
        data.RecordID = $(this).attr('recordid');
        data.IsActive = $(this).attr('isactive');
        data.UserMarketCNName = $(this).attr('usermarketcnname');
        $.post('index.php?s=/home/index/updateamazonauth',data,
            function(res){
                swal({title:'操作成功', text: '3秒钟后自动关闭此窗口，如果您不想等待，直接点击确定关闭', type: 'success', timer: '3000'});
        },'JSON')
    });

    /**
     * 修改店铺名称
     * @author xiaojng
     */
    $('.updateAmazonauthCNName').on('click',function(){
        $('#CNNRecordID').val($(this).attr('recordid'));
        $('#IsActive').val($(this).attr('isactive'))
        $('.UserMarketCNNames').val($(this).attr('usermarketcnname'));
    });

    /**
     * 修改 店铺名称  保存
     * @author xiaojing
     */
    $('#updateMarketCNname').on('click',function(){
        var data  = {};
        data.RecordID = $('#CNNRecordID').val();
        data.UserMarketCNName = $('.UserMarketCNNames').val();
        data.IsActive = $('.IsActive').val();
        $.post('index.php?s=/home/index/updateamazonauth',data,
            function(res){
                swal({title:'保存成功', text: '3秒钟后自动关闭此窗口，如果您不想等待，直接点击确定关闭', type: 'success', timer: '3000'});
            },'JSON')
    });

    /**
     * 删除 店铺
     * @author xiaojing
     */
    $('.deleteAmazonauth').on('click',function(){
        swal({
         title: '确认要执行此操作吗？',//标题
         text: '',//内容
         type: "warning",//类型分别有 success=成功，error=错误，warning警告
         showCancelButton: true,//显示取消按钮
         closeOnConfirm: false,//合上确认
         confirmButtonText: '是的',//确认按钮文本
         confirmButtonColor:'#ec6c62'//确认按钮颜色
         }, function () {
            swal({title:'保存成功', text: '3秒钟后自动关闭此窗口，如果您不想等待，直接点击确定关闭', type: 'success', timer: '3000'});
             /*var data  = {};
                  data.RecordID = $(this).attr('recordid');
             $.post('index.php?s=/home/index/deleteamazonauth',data,
             function(res){
             swal({title:'保存成功', text: '3秒钟后自动关闭此窗口，如果您不想等待，直接点击确定关闭', type: 'success', timer: '3000'});
             },'JSON')*/
         });
    });

    /**
     *  商品调价  ajax 分页加载
     *  @author xiaojin
     */
    $('.pagination').on('click','a',function(){
        var url  = $(this).attr('hrefs');
        var page  = $(this).attr('page');

        $.post(url,{page:page},function(res){
            var pageHtml = res['data'][3];
                pageHtml = pageHtml+ res['data'][1];
                pageHtml = pageHtml+ res['data'][4];
                pageHtml = pageHtml+ res['data'][2];
                pageHtml = pageHtml+ res['data'][5];
               $('.pagination').html(pageHtml);

            var productList = '';
            var datalist = res['data'][8];

            for(var i =0; datalist.length > i; i++){
                productList = productList +'<tr class="trColor">' +
                                            '<td>'+datalist[i]['ProductStatus']+'</td>' +
                                            '<td>'+datalist[i]['BuyBoxStatus']+'</td>' +
                                            ' <td>'+datalist[i]['SellerSKU']+'</td>' +
                                            '<td>'+datalist[i]['DespatchModeType']+'</td>' +
                                            '<td><a href="#" class="shopAndTd">'+datalist[i]['ASIN']+'</a></td>' +
                                            '<td>'+datalist[i]['ProductName']+'</td>' +
                                            '<td>'+datalist[i]['createtime']+'</td>' +
                                            '<td>'+datalist[i]['SaleReportIn30Days']+'</td>';
                if(datalist[i]['DespatchModeType'] == 'FBA'){
                    productList = productList + '<td><span>'+datalist[i]['Quantity']+'</span></td>';
                }else{
                    productList = productList + '<td><input id="inputStyle" type="text" class="productQuantity" value="'+datalist[i]['Quantity']+'"/>' +
                    '<i class="ace-icon fa fa-floppy-o bigger-120 blue saveProductQuantity" style="font-size: 150%!important;"></i>' +
                    '</td>';
                }
                productList = productList +  '<td>'+datalist[i]['RivalQty']+'</td>' +
                                            '<td>'+datalist[i]['BuyBoxPrice']+'</td>' +
                                            '<td>' +
                                            '<input id="inputStyle" type="text" class="savaBtnClick producPrice" value="'+datalist[i]['Price']+'"/>' +
                                            '<i class="ace-icon fa fa-floppy-o bigger-120 blue saveProducPrice" style="font-size: 150%!important;display: none"></i>' +
                                            '<p>(Your Price)++$0 配送费</p>' +
                                            '</td>' +
                                            '</tr>';
            }
            $('#productList').html(productList);
        },'JSON');

    });

    /**
     * 调库存保存
     * @author xiaojing
     */
    $('.saveProductQuantity').on('click',function(){

        //hrefs="index.php?s=/home/index/getajaxproduct" page="2"
        var MarketID =  $(this).parent().prevAll('.productMarketID').val();
        var SellerSKU = $(this).parent().prevAll('.productSellerSKU').val();
        var Quantity = $(this).prev('.productQuantity').val();
        $.post('index.php?s=/home/index/productadjustment',{MarketID:MarketID,SellerSKU:SellerSKU,Quantity:Quantity},function(res){
            var page = $('.pagination').find('.active').find('a');
            page.attr({'hrefs':'index.php?s=/home/index/getajaxproduct','page':page.text()});
            page.trigger('click');
        },'JSON');
    });

    /**
     * 调价保存
     * @author xiaojing
     */
    $('.saveProducPrice').on('click',function(){
        var MarketID = $(this).parent().prevAll('.productMarketID').val();
        var SellerSKU = $(this).parent().prevAll('.productSellerSKU').val();
        var Price = $(this).prev('.producPrice').val();
        $.post('index.php?s=/home/index/productadjustment',{MarketID:MarketID,SellerSKU:SellerSKU,Price:Price},function(res){
            var page = $('.pagination').find('.active').find('a');
            page.attr({'hrefs':'index.php?s=/home/index/getajaxproduct','page':page.text()});
            page.trigger('click');
        },'JSON');
    });
});
